﻿using System;

namespace CubeStone.Wang.Utilities
{
    /// <summary>
    /// 字符串扩展类
    /// </summary>
    public static class StringExtension
    {
        /// <summary>
        /// 获取最大至指定长度的字符串
        /// </summary>
        /// <param name="length">最大长度（大于0）</param>
        /// <returns>经过截取的字符串</returns>
        public static string ToShortString(this string str, int length)
        {
            if(length <= 0)
            {
                throw new Exception("最大长度必须大于0");
            }
            else if (string.IsNullOrEmpty(str))
            {
                return string.Empty;
            }
            else if (str.Length <= length)
            {
                return str;
            }
            else
            {
                return str.Substring(0, length) + "...";
            }
        }
    }
}